//
//  CustomTabBar.m
//  CardMoveGame
//
//  Created by peac on 2025/11/5.
//

#import "CustomTabBar.h"

@interface CustomTabBar ()
@property (nonatomic, strong) NSMutableArray *tabButtons;
@property (nonatomic, strong) UIView *selectionIndicator;
@end
@implementation CustomTabBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
  
    
    // 添加顶部边框
//    UIImageView *tabbgIview=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
//    tabbgIview.image=[UIImage imageNamed:@"bj"];
//    
//    [self addSubview:tabbgIview];
    
    self.tabButtons = [NSMutableArray array];
    self.selectionIndicator = [[UIView alloc] init];
    self.selectionIndicator.backgroundColor = [UIColor blueColor];
    [self addSubview:self.selectionIndicator];
}

- (void)setupTabBarItemsWithTitles:(NSArray *)titles images:(NSArray *)images selectedImages:(NSArray *)selectedImages {
    // 移除现有的按钮
    for (UIButton *button in self.tabButtons) {
        [button removeFromSuperview];
    }
    [self.tabButtons removeAllObjects];
    
    CGFloat buttonWidth = self.frame.size.width / 5.48;
    CGFloat buttonHeight = self.frame.size.height;
    
    for (int i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i==2) {
            button.frame = CGRectMake(i * buttonWidth, 0, buttonWidth*1.48, buttonHeight);
        }else{
            if (i<2) {
                button.frame = CGRectMake(i * buttonWidth,buttonHeight-buttonWidth, buttonWidth, buttonHeight*0.67);
            }else{
                button.frame = CGRectMake((i-1) * buttonWidth+buttonWidth*1.48, buttonHeight-buttonWidth, buttonWidth, buttonHeight*0.67);
            }
        }
   
        button.tag = i;
        
        // 设置图片和文字
 
        [button setBackgroundImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
 
        
        // 调整图片和文字的位置
 
        
        [button addTarget:self action:@selector(tabButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        [self.tabButtons addObject:button];
    }
    
    // 设置选中指示器
//    self.selectionIndicator.frame = CGRectMake(0, 0, buttonWidth, 3);
    [self setSelectedIndex:0];
}

- (void)tabButtonClicked:(UIButton *)sender {
    self.selectedIndex = sender.tag;
    if ([self.delegate respondsToSelector:@selector(tabBarDidSelectItemAtIndex:)]) {
        [self.delegate tabBarDidSelectItemAtIndex:sender.tag];
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    
    for (UIButton *button in self.tabButtons) {
        button.selected = (button.tag == selectedIndex);
    }
    
    // 更新选中指示器位置
//    CGFloat buttonWidth = self.frame.size.width / self.tabButtons.count;
//    [UIView animateWithDuration:0.2 animations:^{
//        self.selectionIndicator.frame = CGRectMake(selectedIndex * buttonWidth, 0, buttonWidth, 3);
//    }];
}

@end
